package org.jeecg.modules.oauth.xkwoauth.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.modules.oauth.xkwoauth.entity.SysThirdOauthRelationship;
import org.jeecg.modules.oauth.xkwoauth.service.ISysThirdOauthRelationshipService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: sys_third_oauth_relationship
 * @author: baochangrong
 * @Date:   2022-08-18
 * @Version: V1.0
 */
@Api(tags="sys_third_oauth_relationship")
@RestController
@RequestMapping("/oauth/xkwoauth/sysThirdOauthRelationship")
@Slf4j
public class SysThirdOauthRelationshipController extends JeecgController<SysThirdOauthRelationship, ISysThirdOauthRelationshipService> {
	@Autowired
	private ISysThirdOauthRelationshipService sysThirdOauthRelationshipService;
	 @Autowired
	 private ISysInfo sysInfo;
	
	/**
	 * 分页列表查询
	 *
	 * @param sysThirdOauthRelationship
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "sys_third_oauth_relationship-分页列表查询")
	@ApiOperation(value="sys_third_oauth_relationship-分页列表查询", notes="sys_third_oauth_relationship-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysThirdOauthRelationship>> queryPageList(SysThirdOauthRelationship sysThirdOauthRelationship,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysThirdOauthRelationship> queryWrapper = QueryGenerator.initQueryWrapper(sysThirdOauthRelationship, req.getParameterMap());
		Page<SysThirdOauthRelationship> page = new Page<SysThirdOauthRelationship>(pageNo, pageSize);
		IPage<SysThirdOauthRelationship> pageList = sysThirdOauthRelationshipService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	 @ApiOperation(value="sys_third_oauth_relationship-根据登录用户查询第三方openid", notes="sys_third_oauth_relationship-根据登录用户查询第三方openid")
	 @GetMapping(value = "/getOpenIdByLoginUser")
	 public Result<SysThirdOauthRelationship> getOpenIdByLoginUser(SysThirdOauthRelationship sysThirdOauthRelationshi,
																   HttpServletRequest req) {
		 BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,sysThirdOauthRelationshi,false);
		 LoginUser loginUser = baseApiEntity.getLoginUser();
		 if(oConvertUtils.isEmpty(loginUser)){
		 	return Result.error("获取登录用户失败，请重新登陆！");
		 }
		 LambdaQueryWrapper<SysThirdOauthRelationship> queryWrapper = new LambdaQueryWrapper<SysThirdOauthRelationship>();
		 queryWrapper.eq(SysThirdOauthRelationship::getSysUserId,loginUser.getId());
		 queryWrapper.eq(SysThirdOauthRelationship::getIzOpen,1);
		 queryWrapper.eq(SysThirdOauthRelationship::getIzDelete,0);
		 //在有效期内,或有效期为null
		 Date date = new Date();
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 String format = sdf.format(date);
		 queryWrapper.and(wrapper  ->wrapper.and(i ->i.le(SysThirdOauthRelationship::getThirdStartTime,format).or().isNull(SysThirdOauthRelationship::getThirdStartTime))
				 .and(j ->j.ge(SysThirdOauthRelationship::getThirdEndTime,format).or().isNull(SysThirdOauthRelationship::getThirdEndTime)));

		 SysThirdOauthRelationship newSysThirdOauthRelationshi = sysThirdOauthRelationshipService.getOne(queryWrapper);
		if(oConvertUtils.isNotEmpty(newSysThirdOauthRelationshi)){
			return Result.OK(newSysThirdOauthRelationshi);
		}
		else{
			return Result.OK("用户未绑定学科网，请绑定后重试");
		}
	 }
	
	/**
	 *   添加
	 *
	 * @param sysThirdOauthRelationship
	 * @return
	 */
	@AutoLog(value = "sys_third_oauth_relationship-添加")
	@ApiOperation(value="sys_third_oauth_relationship-添加", notes="sys_third_oauth_relationship-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SysThirdOauthRelationship sysThirdOauthRelationship) {
		sysThirdOauthRelationshipService.save(sysThirdOauthRelationship);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param sysThirdOauthRelationship
	 * @return
	 */
	@AutoLog(value = "sys_third_oauth_relationship-编辑")
	@ApiOperation(value="sys_third_oauth_relationship-编辑", notes="sys_third_oauth_relationship-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SysThirdOauthRelationship sysThirdOauthRelationship) {
		sysThirdOauthRelationshipService.updateById(sysThirdOauthRelationship);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "sys_third_oauth_relationship-通过id删除")
	@ApiOperation(value="sys_third_oauth_relationship-通过id删除", notes="sys_third_oauth_relationship-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		sysThirdOauthRelationshipService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "sys_third_oauth_relationship-批量删除")
	@ApiOperation(value="sys_third_oauth_relationship-批量删除", notes="sys_third_oauth_relationship-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysThirdOauthRelationshipService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "sys_third_oauth_relationship-通过id查询")
	@ApiOperation(value="sys_third_oauth_relationship-通过id查询", notes="sys_third_oauth_relationship-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysThirdOauthRelationship> queryById(@RequestParam(name="id",required=true) String id) {
		SysThirdOauthRelationship sysThirdOauthRelationship = sysThirdOauthRelationshipService.getById(id);
		if(sysThirdOauthRelationship==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysThirdOauthRelationship);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysThirdOauthRelationship
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysThirdOauthRelationship sysThirdOauthRelationship) {
        return super.exportXls(request, sysThirdOauthRelationship, SysThirdOauthRelationship.class, "sys_third_oauth_relationship");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysThirdOauthRelationship.class);
    }

}
